//
//  ZYVideoLoadingView.m
//  视频播放
//
//  Created by zhangzhenyun on 2018/8/1.
//  Copyright © 2018年 Beijing Youjiu Network. All rights reserved.
//

#import "ZYVideoLoadingView.h"

#define AnimateKey @"rotationAnimation"

@interface ZYVideoLoadingView ()
@property (nonatomic, strong) UIImageView *animateImgView;
@property (nonatomic, strong) UILabel *titleLab;

@end
@implementation ZYVideoLoadingView
//- (instancetype)initWithFrame:(CGRect)frame {
//    self = [super initWithFrame:frame];
//    if (self) {
//        [self setupViews];
//    }
//    return self;
//}
- (instancetype)init {
    if (self = [super init]) {
        [self setupViews];
    }
    return self;
}
- (void)setupViews {
    self.backgroundColor = UIColorFromRGB(0x020202);
    self.layer.cornerRadius =  Fix375(15);
    self.layer.masksToBounds = YES;
    [self addSubview:self.animateImgView];
    [self addSubview:self.titleLab];
    [self setupLayout];
}
- (void)setupLayout {
    [self.animateImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(Fix375(14));
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(Fix375(16), Fix375(16)));
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.animateImgView.mas_right).offset(Fix375(3));
        make.centerY.equalTo(self.animateImgView);
    }];
}
#pragma mark - getter
-(UIImageView *)animateImgView {
    if (_animateImgView == nil) {
        _animateImgView = [[UIImageView alloc] init];
        _animateImgView.image = [UIImage imageNamed:@"Green0"];
    }
    return _animateImgView;
}
-(UILabel *)titleLab {
    if (_titleLab == nil) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.text = @"正在加载";
        _titleLab.font = RMFont(12);
        _titleLab.textColor = [UIColor whiteColor];
    }
    return _titleLab;
}
#pragma mark - animate
/**开始动画*/
- (void)startAnimating {
    self.hidden = NO;
    [self addAnimation];
}
/**结束动画*/
- (void)endAnimating {
    self.hidden = YES;
    [self.animateImgView.layer removeAnimationForKey:AnimateKey];
}
/**添加动画*/
- (void)addAnimation {
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    [self.animateImgView.layer addAnimation:rotationAnimation forKey:AnimateKey];
}

@end
